#!/usr/bin/evn python
# -*- coding: UTF-8 -*-
# @File: wechat_corp_tag.py
# @Author: fy
# @date: 2021/10/24 20:02
from apis.wechat import WeChat
from  utils.common_fun import read_tokens_yaml


class WeChatCorpTag(WeChat):
    '''
    管理企业标签
    '''

    def __init__(self):
        datas = read_tokens_yaml()
        corpid = datas.get('corp_tag_list').get('corpid')
        corp_tag_secret = datas.get('corp_tag_list').get('corp_tag_secret')
        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corp_tag_secret}"
        resp = self.send("GET", url)
        self.token = resp.json()['access_token']

    def add_corp_tag(self, data):
        '''
        添加企业客户标签
        :param data:
        :return:
        '''
        if self.token is None:
            self.get_corp_list_tag_token()
        url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag?access_token={self.token}"
        return self.send("POST", url, json=data)

    def update_corp_tag(self, data):
        '''
        编辑企业客户标签
        :param data:
        :return:
        '''
        if self.token is None:
            self.get_corp_list_tag_token()
        url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag?access_token={self.token}"
        return self.send("POST", url, json=data)

    def get_corp_tag(self, *data):
        '''
        获取企业客户标签列表
        :param data:
        :return:
        '''
        if self.token is None:
            self.get_corp_list_tag_token()
        url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?access_token={self.token}"
        return self.send("POST", url, json=data)

    def get_corp_list_group_id_list(self):
        '''
         获取全部的企业客户标签的组id
        :return:
        '''
        resp = self.get_corp_tag()
        resp = resp.json()
        tag_group_list = resp['tag_group']
        group_id_list = []
        for group in tag_group_list:
            if group["group_id"] is not None:
                group_id_list.append(group["group_id"])
                pass
            pass
        return group_id_list

    def get_group_id_by_group_name(self, group_name):
        '''
        通过标签组名 获取组id
        :param group_name:
        :return:
        '''
        resp = self.get_corp_tag()
        resp = resp.json()
        group_list = resp['tag_group']
        for group in group_list:
            if group["group_name"] == group_name:
                return group["group_id"]
                break
        return None

    def get_tag_id_by_tag_name(self, tag_name):
        '''
        通过客户标签名  获取 客户标签id
        :param tag_name:
        :return:
        '''
        resp = self.get_corp_tag()
        resp = resp.json()
        tag_id_list = []
        group_list = resp['tag_group']
        for group in group_list:
            tag_list = group['tag']
            for tag in tag_list:
                if tag["name"] == tag_name:
                    tag_id_list.append(tag["id"])
                    pass
                break
                pass
            pass
        return tag_id_list

    def delete_corp_list(self, data):
        '''
        删除企业客户标签
        :param data: tag_id和group_id不可同时为空。
        :return:
        '''
        if self.token is None:
            self.get_corp_list_tag_token()
        url = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag?access_token={self.token}"
        return self.send("POST", url, json=data)
